<?php

class AdminController extends Controller {

    public function loginAction() {
        include CURRENT_VIEW_PATH . 'login.html';
    }

    public function signinAction() {
        session_start();
        //校验验证码
        $captcha = strtolower(trim($_POST['captcha']));
        if($captcha != $_SESSION['captcha']) {
            $this->_jump("index.php?p=back&c=admin&a=login", "验证码不正确", 2);
        }

        //1.获取表单提交数据
//        session_start();
        $username = trim($_POST['username']);
        $password = md5(trim($_POST['password']));

        //转义数据
        $username = addslashes($username);
        $password = addslashes($password);

        //2.判断数据可用性

        //3.提交数据，进行判断
        $adminModel = Factory::M("AdminModel");
        $userinfo = $adminModel->getUserInfo($username, $password);
        if(empty($userinfo)) {
            $this->_jump("index.php?p=back&c=admin&a=login", "用户名或密码错误，请重试");
        }
        else {
            $_SESSION['admin'] = $userinfo;
            $this->_jump("index.php?p=back&c=manage&a=index");
        }
    }

    //注销
    public function logoutAction(){
        //销毁session
        unset($_SESSION['admin']);
        session_destroy();
        $this->_jump('index.php?p=back&c=admin&a=login');
    }

    public function captchaAction() {
        $c = new Captcha();
        $c->generateCode();
        session_start();
        $_SESSION['captcha'] = $c->getCode();
    }

}